package com.scqkxx.gamebox.activity.activities;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.avos.avoscloud.AVObject;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.presenter.ActivePersenter;
import com.scqkxx.gamebox.activity.activities.utils.SimplePaddingDecoration;
import com.scqkxx.gamebox.activity.activities.adapter.ActiveAdapter;
import com.scqkxx.gamebox.activity.activities.view.ActiveView;
import com.scqkxx.gamebox.activity.activities.view.TitleView;
import com.scqkxx.gamebox.databinding.ActivityActiveBinding;
import com.scqkzqtz.base.base.BaseFragment;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动
 * Created by hef on 2018/8/10.
 */

public class ActivitiesFragment extends BaseFragment<ActivityActiveBinding> implements ActiveView,
       TitleView.onTitleClickListener, OnRefreshListener, OnLoadMoreListener {
    private ActiveAdapter mActiveAdapter; //活动页面适配器
    private List<AVObject> mActiveList; //数据源
    private ActivePersenter mActivePersenter; //活动数据的控制器
    private int pagerNumber = 1; //当前翻页的页数
    private View view ,view2 ;

    //自动刷新
    @Override
    public void loadData() { binding.mSmartRefreshLayout.autoRefresh(0  ,250 ,1f); }

    @Override
    public int setLayoutId() { return  R.layout.activity_active ; }

    @Override
    public void init() { initData();  initListener(); }

    //初始化
    private void initData() {
        view  = inflate(com.scqkzqtz.base.R.layout.layout_error);
        view2  = inflate(com.scqkzqtz.base.R.layout.layout_empty);
        mActivePersenter = new ActivePersenter(_Activity ,this );
        mActiveList = new ArrayList<>();
        mActiveAdapter = new ActiveAdapter( R.layout.active_layout_item , mActiveList );
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(_Activity);
        SimplePaddingDecoration mSimplePaddingDecoration = new SimplePaddingDecoration(_Activity);
        binding.mRecyclerView.setLayoutManager(mLinearLayoutManager);
        binding.mRecyclerView.addItemDecoration(mSimplePaddingDecoration);
        binding.mRecyclerView.setAdapter(mActiveAdapter);
    }

    //设置监听
    private void initListener() {
        mActivePersenter.addOnItemTouchListener( binding.mRecyclerView );
        mActivePersenter.OnItemChildClickListener( binding.mRecyclerView );
        binding.mTitleView.setOnTitleClickListener(this);
        binding.mSmartRefreshLayout.setOnRefreshListener(this);
        binding.mSmartRefreshLayout.setOnLoadMoreListener(this);
    }

    //加载更多
    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout ) {
        mActivePersenter.sendHttpActiveData(pagerNumber);
    }

    //上啦刷新
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pagerNumber = 1;
        mActivePersenter.sendHttpActiveData(pagerNumber);
    }

    //请求成功
    @Override
    public void onActiveSuccess( List<AVObject> list ) { mActiveList.addAll(list); }

    //关闭刷新
    @Override
    public void onFinishFreshAndLoad( boolean isLoad ) {
        binding.mSmartRefreshLayout.finishRefresh ( isLoad );
        binding.mSmartRefreshLayout.finishLoadMore( isLoad );
    }

    // 设置pagenumber
    @Override
    public void setPageNumber() { pagerNumber ++ ; }

    //清除数据
    @Override
    public void clearActiveList() {
        mActiveList.clear();
    }

    //更新数据
    @Override
    public void loadMoreComplete() {
        mActiveAdapter.loadMoreComplete();
    }

    //无数据
    @Override
    public void setEmptyStateView( ) { //mActiveAdapter.setEmptyView(view2);
         }

    //没有更多
    @Override
    public void loadMoreEnd() { mActiveAdapter.loadMoreEnd(); }

    //商城的监听
    @Override
    public void setOnTitleShoppingClick() { }

    // 客服的监听
    @Override
    public void setOnTitleServiceClick() { }

    @Override
    public void onFinishActivity() {
        if (_Activity != null) _Activity.finish(); else getActivity().finish();
    }

    //设置监听
    @Override
    public void setOnTitleSettingClick() { }

    //返回当前数据的size
    @Override
    public int getListSize() {
        return mActiveList.size();
    }

    //设置网络异常的view
    @Override
    public void setErrorStateView() {
        //mActiveAdapter.setEmptyView(view);
    }

    //加载失败
    @Override
    public void loadMoreFail() { mActiveAdapter.loadMoreFail(); }

    //title 的监听
    @Override
    public void onTitleClick( int mTitleType ) {
        mActivePersenter.setOnTitleClick(mTitleType);
    }

}
